home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / port / svr4 / sys / byteorder.h
C/C++ Source or Header  |  1994-08-01  |  3KB  |  132 lines

  1. #ifndef _SYS_BYTEORDER_H
  2. #define _SYS_BYTEORDER_H
  3.  
  4. /* Functions to convert `short' and `long' quantities from host byte order
  5.    to (internet) network byte order (i.e. big-endian).
  6.  
  7.    Written by Ron Guilmette (rfg@ncd.com).
  8.  
  9.    This isn't actually used by GCC.  It is installed by fixinc.svr4.
  10.  
  11.    For big-endian machines these functions are essentially no-ops.
  12.  
  13.    For little-endian machines, we define the functions using specialized
  14.    asm sequences in cases where doing so yields better code (e.g. i386).  */
  15.  
  16. #if !defined (__GNUC__) && !defined (__GNUG__)
  17. #error You lose!  This file is only useful with GNU compilers.
  18. #endif
  19.  
  20. #ifdef __GNUC__
  21. #define __STATIC static
  22. #else
  23. #define __STATIC
  24. #endif
  25.  
  26. #ifdef __STDC__
  27. __STATIC __inline__ unsigned long htonl (unsigned long);
  28. __STATIC __inline__ unsigned short htons (unsigned int);
  29. __STATIC __inline__ unsigned long ntohl (unsigned long);
  30. __STATIC __inline__ unsigned short ntohs (unsigned int);
  31. #endif /* defined (__STDC__) */
  32.  
  33. #if defined (__i386__)
  34.  
  35. /* Convert a host long to a network long.  */
  36.  
  37. __STATIC __inline__ unsigned long
  38. htonl (unsigned long __arg)
  39. {
  40.   register unsigned long __result;
  41.  
  42.   __asm__ ("xchg%B0 %b0,%h0\n\
  43.     ror%L0 $16,%0\n\
  44.     xchg%B0 %b0,%h0" : "=q" (__result) : "0" (__arg));
  45.   return __result;
  46. }
  47.  
  48. /* Convert a host short to a network short.  */
  49.  
  50. __STATIC __inline__ unsigned short
  51. htons (unsigned int __arg)
  52. {
  53.   register unsigned short __result;
  54.  
  55.   __asm__ ("xchg%B0 %b0,%h0" : "=q" (__result) : "0" (__arg));
  56.   return __result;
  57. }
  58.  
  59. #elif ((defined (__i860__) && !defined (__i860_big_endian__))    \
  60.        || defined (__ns32k__) || defined (__vax__)        \
  61.        || defined (__spur__) || defined (__arm__))
  62.  
  63. /* For other little-endian machines, using C code is just as efficient as
  64.    using assembly code.  */
  65.  
  66. /* Convert a host long to a network long.  */
  67.  
  68. __STATIC __inline__ unsigned long
  69. htonl (unsigned long __arg)
  70. {
  71.   register unsigned long __result;
  72.  
  73.   __result = (__arg >> 24) & 0x000000ff;
  74.   __result |= (__arg >> 8) & 0x0000ff00;
  75.   __result |= (__arg << 8) & 0x00ff0000;
  76.   __result |= (__arg << 24) & 0xff000000;
  77.   return __result;
  78. }
  79.  
  80. /* Convert a host short to a network short.  */
  81.  
  82. __STATIC __inline__ unsigned short
  83. htons (unsigned int __arg)
  84. {
  85.   register unsigned short __result;
  86.  
  87.   __result = (__arg << 8) & 0xff00;
  88.   __result |= (__arg >> 8) & 0x00ff;
  89.   return __result;
  90. }
  91.  
  92. #else /* must be a big-endian machine */
  93.  
  94. /* Convert a host long to a network long.  */
  95.  
  96. __STATIC __inline__ unsigned long
  97. htonl (unsigned long __arg)
  98. {
  99.   return __arg;
  100. }
  101.  
  102. /* Convert a host short to a network short.  */
  103.  
  104. __STATIC __inline__ unsigned short
  105. htons (unsigned int __arg)
  106. {
  107.   return __arg;
  108. }
  109.  
  110. #endif /* big-endian */
  111.  
  112. /* Convert a network long to a host long.  */
  113.  
  114. __STATIC __inline__ unsigned long
  115. ntohl (unsigned long __arg)
  116. {
  117.   return htonl (__arg);
  118. }
  119.  
  120. /* Convert a network short to a host short.  */
  121.  
  122. __STATIC __inline__ unsigned short
  123. ntohs (unsigned int __arg)
  124. {
  125.   return htons (__arg);
  126. }
  127.  
  128.  
  129. #undef __STATIC
  130.  
  131. #endif /* !defined (_SYS_BYTEORDER_H) */
  132.